package com.hicorp.segment.controller;


import com.hicorp.segment.pojo.bo.UserChangePassword;
import com.hicorp.segment.pojo.po.*;
import com.hicorp.segment.service.AuthorityService;
import com.hicorp.segment.service.BaseInterface;
import com.hicorp.segment.utils.ResultBean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/system")
public class SystemController {
    private final BaseInterface baseInterface;

    private final AuthorityService authorityService;

    public SystemController(AuthorityService authorityService, BaseInterface baseInterface) {
        this.authorityService = authorityService;
        this.baseInterface = baseInterface;
    }


    @PostMapping("/{tableName}")
    public Object createRecord(@PathVariable String tableName, @RequestBody User user) {
//        System.out.println(tableName);
        return authorityService.register(user);
    }

    @PutMapping("/{tableName}/{id}")
    public Object updateRecord(@PathVariable String tableName, @PathVariable Long id, @RequestBody Object record) {
        return baseInterface.update(tableName, id, record);
    }


    @GetMapping("/selectAllMenuTree")
    public ResultBean<List<Menu>> selectAllMenuTree() {
        return authorityService.selectAllMenuTree();
    }

    @PostMapping("/selectMenuBackendApiTree")
    public ResultBean<List<Menu>> selectMenuBackendApiTree(@RequestBody Long roleId) {
        return authorityService.selectMenuBackendApiTree(roleId);
    }

    @PostMapping("/setRoleMenuRelation")
    public ResultBean<Void> setRoleMenuRelation(@RequestBody List<RoleMenuRelation> roleMenuRelations) {
        return authorityService.setRoleMenuRelation(roleMenuRelations);
    }

    @PostMapping("/setRoleBackendApiRelation")
    public ResultBean<Void> setRoleBackendApiRelation(@RequestBody List<RoleBackendApiRelation> roleBackendApiRelations) {
        return authorityService.setRoleBackendApiRelation(roleBackendApiRelations);
    }

    @PostMapping("/setMenuBackendApiRelation")
    public ResultBean<Void> setMenuBackendApiRelation(@RequestBody List<MenuBackendApiRelation> menuBackendApiRelationList) {
        return authorityService.setMenuBackendApiRelation(menuBackendApiRelationList);
    }

    @PostMapping("/setUserRoleRelation")
    public ResultBean<Void> setUserRoleRelation(@RequestBody List<UserRoleRelation> userRoleRelations) {
        return authorityService.setUserRoleRelation(userRoleRelations);
    }

    @GetMapping("/selectMenuIdByRoleId/{roleId}")
    public ResultBean<List<Long>> selectMenuIdByRoleId(@PathVariable Long roleId) {
        return authorityService.selectMenuIdByRoleId(roleId);
    }
    @GetMapping("/selectBackendAPiIdByRoleId/{roleId}")
    public ResultBean<List<Long>> selectBackendAPiIdByRoleId(@PathVariable Long roleId) {
        return authorityService.selectBackendAPiIdByRoleId(roleId);
    }

    @GetMapping("/selectRoleIdByUserId/{userId}")
    public ResultBean<List<Long>> selectRoleIdByUserId(@PathVariable Long userId) {
        return authorityService.selectRoleIdByUserId(userId);
    }

    //    @GetMapping("/selectResourceByMenuId/{menuId}")
//    public ResultBean<List<BackendApi>> selectResourceByMenuId(@PathVariable Long menuId) {
//        return authorityService.selectResourceByMenuId(menuId);
//    }
//    @PostMapping ("/selectResourceByMenuIdList")
//    public ResultBean<List<Long>> selectResourceByMenuIdList(@RequestBody List<Long> menuIdList) {
//        System.out.println("aasdaaaaaaaaaaa");
//        return authorityService.selectResourceByMenuId(menuIdList);
//    }
    @GetMapping("/selectPhoneBackendApi")
    public ResultBean<List<BackendApi>> selectBackendApiByMenuId() {
        return authorityService.selectPhoneBackendApi();
    }

    @PostMapping("/selectBackendApiByMenuId/{menuId}")
    public ResultBean<List<BackendApi>> selectBackendApiByMenuId(@PathVariable Long menuId) {
        return authorityService.selectBackendApiByMenuId(menuId);
    }
    //管理员重置某用户密码
    @PostMapping("/resetPassword/{password}/{resetUserId}")
    public ResultBean<Integer> resetPassword(@PathVariable Long resetUserId, @PathVariable String password) {
        return authorityService.resetPassword(resetUserId, password);
    }


    //用户本人修改密码
    @PostMapping("/changePassword")
    public ResultBean<Integer> changePassword(@RequestBody UserChangePassword userChangePassword) {
        System.out.println(userChangePassword);
        return authorityService.changePassword(userChangePassword);
    }
    //用户更换头像
    @PostMapping("/upload-head-portrait")
    public ResultBean<String> uploadHeadPortrait(@RequestParam("file") MultipartFile file) {
        return authorityService.uploadHeadPortrait(file);
    }

}
